import ResHelper from "../Utilities/ResHelper";
import PresistNode from "../Utilities/PresistNode";
import CalcHelper from "./CalcHelper";
import SoundHelper from "./SoundHelper";
import BattleScene from "../BattleScene";
import AStar from "./AStar";
import MainPlayer from "../Entity/MainPlayer";
import UISetting from "../UI/UISetting";
import C from "../Configs/Config";
import UISplash from "../UI/UISplash";
import Constants from "../Constants/Constants";
/**
 * 公共资源类,需要四处访问的,请找我
 * created by Five on 2018-11-5 12:09:42
 */

const { ccclass } = cc._decorator;

@ccclass
class Global extends cc.Component {
    private static instance: Global = null;

    public static Ins() {
        if (this.instance == null) {
            this.instance = PresistNode("global").addComponent(Global);
            this.instance.init();
        }
        return this.instance;
    }

    public Res: ResHelper = null;
    public Calc: CalcHelper = null;
    public Sound: SoundHelper = null;
    public AStar: AStar = null;
    public CurScene: BattleScene = null;
    public SelectedMap: number = -1;
    // 主角
    public MainPlayer: MainPlayer = null;
    public TestOri: boolean = false;
    public CurrentMap: number = 0;
    public Setting: UISetting = null;
    public Splash: UISplash = null;

    private init(): void {
        this.Sound = new SoundHelper();
        this.Res = new ResHelper();
        this.Calc = new CalcHelper();
        this.newPublicForm();
    }

    /**加载必要的文件 */
    public Init(): void {
        this.AStar = new AStar();
        this.SelectedMap = -1;
        this.MainPlayer = null;
        this.CurScene = null;
    }

    private async newPublicForm() {
        if (this.Setting == null) {
            let node = PresistNode("setting");
            let child = await this.Res.GetRes(Constants.PRE_SETTING) as cc.Node;
            let size = cc.winSize;
            child.position = cc.v2(size.width / 2, size.height / 2);
            node.addChild(child, 9999);
            this.Setting = child.getComponent(UISetting);
        }
        if (this.Splash == null) {
            let node = PresistNode("splash");
            let child = await this.Res.GetRes(Constants.PRE_SPLASH) as cc.Node;
            node.addChild(child, 9999);
            this.Splash = child.getComponent(UISplash);
        }
    }

    private async newSplahForm() {
    }

    public ToMainScene() {
        cc.director.loadScene("MainScene", () => {
        });
    }

    public ToBattleScene(map: number, editMode: boolean) {
        cc.director.loadScene("BattleScene", () => {
            let scene = cc.director.getScene();
            let canvas = scene.getChildByName("Canvas");
            canvas.getComponent(BattleScene).LoadMap(map, editMode);
        });
        if (!editMode)
            this.ShowSplash(map);
    }

    public async ShowSplash(index: number) {
        await this.newSplahForm();
        let mData = C().Map.GetMapByIndex(index);
        if (mData) {
            this.Splash.setName(mData.MapName, () => {
            });
        } else {
            this.Splash.setName(C().Map.GetNameByIndex(index), () => {
            });
        }
    }

    public HidSplash() {
        this.Splash.close();
    }
}

export default function G() {
    return Global.Ins();
}